/**
 * Created by thinkjoy on 2017/7/21.
 */
$(function () {
    $('#adduserbtn').click(function () {
        var content=$('#addtipbtn');
        layui.use('layer', function(){
            layer.open({
                type: 1,
                skin:'layui-layer-molv',
                title:  ['添加用户(单个/批量)', 'font-size:18px;'],
                closeBtn: 2,
                content:content,
                area:  ['450px', '350px'],
                shade: 0.8,
                shadeClose:true,
                resize:true,
                anim:1,
                id: 'LAY_layuipro', //设定一个id，防止重复弹出
                moveType: 1
            })
        });
    });
    $('#singlebtn').click(function () {
        var content=$('#addsingleuser');
        layui.use('layer', function(){
            layer.open({
                type: 1,
                skin:'layui-layer-molv',
                title:  ['添加新用户', 'font-size:18px;'],
                closeBtn: 1,
                shade: 0,
                content:content,
                area:  ['100%', '100%'],
                shadeClose:true,
                resize:true,
                anim:1,
                id: 'LAY_layuipro1', //设定一个id，防止重复弹出
                moveType: 1,
                maxmin: false
            });
        });
    });

    $('#mulbtn').click(function () {
        var contennt=$('#addmuluser');
        layui.use('layer', function(){
            layer.open({
                type: 1,
                skin:'layui-layer-molv',
                title:  ['批量添加用户', 'font-size:18px;'],
                closeBtn: 1,
                shade: 0,
                content:contennt,
                area:  ['100%', '100%'],
                shadeClose:true,
                resize:true,
                anim:1,
                id: 'LAY_layuipro2', //设定一个id，防止重复弹出
                moveType: 1,
                maxmin: false
            });
        });
    });

    $('#defaultbtn').click(function () {
        var pwd=$('#accountid').val();
        if(pwd!=''){
            $('#password').val(pwd);
        }else{
            layer.msg("请先输入账号");
        }
    });

    $('#checkmulbtn').click(function () {
        var content=$('#filetip').text();
        if (content!=''){
            var path=$('#filepath').text();
            var params=$.param({
                filepath:path
            },true);
            $.ajax({
                url:'/admin/user/addmuluser',
                type:'post',
                data:params,
                dataType:'json',
                success:function (e) {
                    if(e=='0'){
                        layer.msg("数据导入成功");
                        $('#filetip').text('');
                        window.location.href="/admin/user/userlist";
                    }else if(e=='1'){
                        layer.msg("excel文件格式不符合要求,请重新导入");
                        $('#filetip').text('')
                    }else if(e=='2'){
                        layer.msg("发生未知错误,请重试");
                    }else {
                        var responseObj=JSON.stringify(e);
                        var contents='导入失败，部分账号已存在，请更换账号再次导入,重复的账号是：'+responseObj;
                        layer.msg(contents);
                    }
                }
            })
        }else {
            layer.msg('请先导入文件');
        }
    });

    // 详情
    $("a[name$='userdetail']").click(function () {
        var accountid=$(this).attr('accountid');
        var params=$.param({
            accountid:accountid
        },true);
        $.ajax({
            url:'/admin/user/detail',
            type:'post',
            data:params,
            dataType:'json',
            success:function (e) {
                // console.log(e);
                var responseObj=eval('('+'e'+')');
                var content=$('#userdetail');
                layui.use('layer', function(){
                    layer.open({
                        type: 1,
                        skin:'layui-layer-molv',
                        title:  ['用户  :  '+responseObj.userInfo.username+'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' +
                        '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'+'创建时间   :   '+responseObj.userInfo.createtime, 'font-size:18px;'],
                        closeBtn: 1,
                        shade: 0,
                        content:content,
                        area:  ['100%', '100%'],
                        shadeClose:true,
                        resize:true,
                        anim:1,
                        id: 'LAY_layuipro6', //设定一个id，防止重复弹出
                        moveType: 1,
                        maxmin: false,
                        success:function (layero, index) {
                            $('#userphoto').attr('src',responseObj.userInfo.photo);
                            $('#myaccountid').val(responseObj.userInfo.accountid);
                            $('#myage').val(responseObj.userInfo.age);
                            $('#myusername').val(responseObj.userInfo.username);
                            $('#myphone').val(responseObj.userInfo.phone);
                            $('#mymail').val(responseObj.userInfo.mail);
                            $('#myaddress').val(responseObj.userInfo.address);
                            $('#myphoto').val(responseObj.userInfo.photo);
                            $('#myrole').empty();
                            $.each(responseObj.raRoles,function (i,value) {
                                $('#myrole').append("<option value='"+value.id+"'>"+value.role+"</option>");
                            });
                            layui.use('form', function() {
                                var form = layui.form;
                                $('[name="sex"][value="' + responseObj.userInfo.sex + '"]').prop('checked', true);//改变radio选择
                                $('#myrole').val(responseObj.userInfo.roleid);
                                $('#open').prop('checked',false);
                                if(responseObj.userInfo.roleid=='1'){
                                    $('#detailhead').hide();
                                }else{
                                    $('#detailhead').show();
                                }
                                form.render();
                                form.on('submit(userdetailbtn)', function(data){
                                    $.ajax({
                                        url:'/admin/person/updateperson',
                                        type:'post',
                                        data:data.field,
                                        dataType:'json',
                                        success:function (e) {
                                            if(e=='0'){
                                                layer.msg("修改成功");
                                                window.location.href="/admin/user/userlist";
                                            }else{
                                                layer.msg("修改失败，请重试");
                                            }
                                        }
                                    });
                                    return false;
                                });

                                // 用户详情修改密码
                                $('#btnupdatepwd').click(function () {
                                    if($(this).prop("disabled")==false){
                                        var mycontent=$('#updatepwddetail');
                                        layui.use('layer', function(){
                                            layer.open({
                                                type: 1,
                                                skin:'layui-layer-molv',
                                                title:  ['修改密码', 'font-size:18px;'],
                                                closeBtn: 2,
                                                content:mycontent,
                                                area:  ['500px', '180px'],
                                                shade: 0.8,
                                                btn: ['确认修改', '我怂了'],
                                                shadeClose:true,
                                                resize:true,
                                                anim:1,
                                                id: 'LAY_layuipro10', //设定一个id，防止重复弹出
                                                moveType: 1,
                                                success:function (layero, index) {
                                                    $('#mypassword').val('');
                                                },
                                                yes:function (index, layero) {
                                                    var password=$('#mypassword').val();
                                                    var newparam=$.param({
                                                        password:password,
                                                        accountid:responseObj.userInfo.accountid
                                                    },true);
                                                    $.ajax({
                                                        url:'/admin/user/detail/updatepwd',
                                                        type:'post',
                                                        data:newparam,
                                                        dataType:'json',
                                                        success:function (e) {
                                                            if(e=='0'){
                                                                layer.msg('密码修改成功');
                                                            }else {
                                                                layer.msg('密码修改失败,请重试!');
                                                            }
                                                        }
                                                    });
                                                    layer.close(index);
                                                }
                                            })
                                        });
                                    }
                                });

                                // 开关
                                form.on('switch(switchfilter)', function(data){
                                    if(data.elem.checked){
                                        $('#userdetailbtn').removeAttr("disabled");
                                        $('#userdetailbtn').removeClass("layui-btn-disabled");
                                        $('#btnupdatepwd').prop("disabled",false);
                                        $('#btnupdatepwd').removeClass("layui-btn-disabled");
                                        $('#btnupdatepwd').addClass("layui-btn-danger");
                                        $('#myrole').removeAttr("disabled");
                                        $('#myage').removeAttr("disabled");
                                        $('input[name="sex"]').removeAttr("disabled");
                                        $('#myusername').removeAttr("disabled");
                                        $('#myphone').removeAttr("disabled");
                                        $('#mymail').removeAttr("disabled");
                                        $('#myaddress').removeAttr("disabled");
                                        $('#myrole').removeAttr("disabled");
                                        $('#myuploadfile').removeAttr("disabled");
                                        layer.msg("已开启修改");
                                    }else {
                                        $('#userdetailbtn').attr("disabled",true);
                                        $('#userdetailbtn').addClass("layui-btn-disabled");
                                        $('#btnupdatepwd').prop("disabled",true);
                                        $('#btnupdatepwd').removeClass("layui-btn-danger");
                                        $('#btnupdatepwd').addClass("layui-btn-disabled");
                                        $('#myrole').attr("disabled",true);
                                        $('#myage').attr("disabled",true);
                                        $('input[name="sex"]').attr("disabled",true);
                                        $('#myusername').attr("disabled",true);
                                        $('#myphone').attr("disabled",true);
                                        $('#mymail').attr("disabled",true);
                                        $('#myaddress').attr("disabled",true);
                                        $('#myrole').attr("disabled",true);
                                        $('#myuploadfile').attr("disabled",true);
                                        layer.msg("已关闭修改，若当前修改未提交，则修改无效");
                                    }
                                    form.render();
                                });

                                // 登录日志显示
                                if(responseObj.sysUserLogs.length==0){
                                    $('#logdiv').hide();
                                    layer.msg('该账号还未登录过');
                                }else{
                                    $('#logdiv').show();
                                    var tablebody=$('#logtable tbody');
                                    tablebody.html('');
                                    // 显示前10条数据
                                    if(responseObj.sysUserLogs.length>=10){
                                        for(var i=0;i<10;i++){
                                            tablebody.append("<tr>" +
                                                "<td>"+(i+1)+"</td>" +
                                                "<td>"+responseObj.sysUserLogs[i].accountid+"</td>" +
                                                "<td>"+responseObj.sysUserLogs[i].logindate+"</td>" +
                                                "<td>"+responseObj.sysUserLogs[i].ip+"</td>" +
                                                "</tr>");
                                        }
                                    }else{
                                        for(var i=0;i<responseObj.sysUserLogs.length;i++){
                                            tablebody.append("<tr>" +
                                                "<td>"+(i+1)+"</td>" +
                                                "<td>"+responseObj.sysUserLogs[i].accountid+"</td>" +
                                                "<td>"+responseObj.sysUserLogs[i].logindate+"</td>" +
                                                "<td>"+responseObj.sysUserLogs[i].ip+"</td>" +
                                                "</tr>");
                                        }
                                    }
                                }

                                // 更多
                                $('#more').click(function () {
                                    layer.open({
                                        type: 1,
                                        skin:'layui-layer-molv',
                                        title:  ['更多日志', 'font-size:18px;'],
                                        closeBtn: 2,
                                        content:$('#logdetail'),
                                        area:  ['650px', '800px'],
                                        shade: 0.8,
                                        shadeClose:true,
                                        resize:true,
                                        anim:1,
                                        id: 'LAY_layuipro11', //设定一个id，防止重复弹出
                                        moveType: 1,
                                        success:function(){
                                            var tablebodydetail=$('#logtabledetail tbody');
                                            tablebodydetail.html('');
                                            for(var i=0;i<responseObj.sysUserLogs.length;i++){
                                                tablebodydetail.append("<tr>" +
                                                    "<td>"+(i+1)+"</td>" +
                                                    "<td>"+responseObj.sysUserLogs[i].accountid+"</td>" +
                                                    "<td>"+responseObj.sysUserLogs[i].logindate+"</td>" +
                                                    "<td>"+responseObj.sysUserLogs[i].ip+"</td>" +
                                                    "</tr>");
                                            }
                                        }
                                    })
                                });

                            });
                        },
                        cancel:function (index, layero) {
                            $('#userdetailbtn').attr("disabled",true);
                            $('#userdetailbtn').addClass("layui-btn-disabled");
                            $('#btnupdatepwd').prop("disabled",true);
                            $('#btnupdatepwd').removeClass("layui-btn-danger");
                            $('#btnupdatepwd').addClass("layui-btn-disabled");
                            $('#myrole').attr("disabled",true);
                            $('#myage').attr("disabled",true);
                            $('input[name="sex"]').attr("disabled",true);
                            $('#myusername').attr("disabled",true);
                            $('#myphone').attr("disabled",true);
                            $('#mymail').attr("disabled",true);
                            $('#myaddress').attr("disabled",true);
                            $('#myrole').attr("disabled",true);
                            $('#myuploadfile').attr("disabled",true);
                        }
                    });
                });
            }
        });
    });

    //冻结
    $("a[name$='dongjie']").click(function () {
        var accountid=$(this).attr('accountid');
        var delflag=$(this).attr('delflag');
        var params=$.param({
            accountid:accountid,
            delflag:delflag
        },true);
        $.ajax({
            url:'/admin/user/frozenuser',
            type:'post',
            data:params,
            dataType:'json',
            success:function (e) {
                if(e=='0'){
                    window.location.href='/admin/user/userlist';
                }else if(e=='1'){
                    window.location.href='/admin/user/userlist';
                }else if(e=='2'){
                    layer.msg('该账户冻结失败');
                }else if(e=='3'){
                    layer.msg('该账户解冻失败');
                }
            }
        })
    });

    //删除
    $("a[name$='deluser']").click(function () {
        var accountid=$(this).attr('accountid');
        layer.open({
            type: 1,
            title: false,
            closeBtn: 2,
            resize:true,
            content:'<div style="padding: 50px; line-height: 22px; background-color: #393D49; color: #fff; font-weight: 300;">' +
            '是否需要删除?删除后将不可恢复</div>',
            area:  '340px',
            shade: 0.8,
            shadeClose:true,
            anim:1,
            id: 'LAY_layuipro5', //设定一个id，防止重复弹出
            btn: ['绝不后悔', '我怂了'],
            moveType: 1,
            yes:function (index, layero) {
                var params=$.param({
                    accountid:accountid
                },true);
                $.ajax({
                    url:'/admin/user/deluser',
                    type:'post',
                    data:params,
                    dataType:'json',
                    success:function (e) {
                        if(e=='0'){
                            window.location.href='/admin/user/userlist';
                        }else {
                            layer.msg('删除失败');
                        }
                        layer.close(index);
                    }
                })
            }
        })
    });

});

layui.use('form', function(){
    var form = layui.form;
    //监听提交
    form.on('submit(formDemo)', function(data){
        $.ajax({
            url:'/admin/user/addsingleuser',
            type:'post',
            data:data.field,
            dataType:'json',
            success:function (e) {
                if(e=='0'){
                    window.location.href="/admin/user/userlist";
                }else if(e=='1'){
                    layer.msg("添加失败，请重试");
                }else if(e=='2'){
                    layer.msg("账号已存在，请换一个账号");
                }
            }
        });
        return false;
    });
});

layui.use('upload', function(){
    var upload = layui.upload;
    // 上传图片
    upload.render({
        url: '/admin/person/uploadmyphoto',
        elem: '#uploadfile',
        method: 'post',
        accept:'images',
        done: function(res){
            var responseObj=eval('('+'res'+')');
            var code=responseObj.code;
            if(code==0){
                var photourl=responseObj.data.access_url;
                $('#personphoto').attr('src',photourl);
                $('#photo').val(photourl);
                layer.msg("上传成功");
            }else{
                layer.msg("上传失敗");
            }
        }
    });

    upload.render({
        url: '/admin/person/uploadmyphoto',
        elem: '#myuploadfile',
        method: 'post',
        accept:'images',
        done: function(res){
            var responseObj=eval('('+'res'+')');
            var code=responseObj.code;
            if(code==0){
                var photourl=responseObj.data.access_url;
                $('#userphoto').attr('src',photourl);
                $('#myphoto').val(photourl);
                layer.msg("上传成功");
            }else{
                layer.msg("上传失敗");
            }
        }
    });

    // 上传excel
    upload.render({
        url: '/admin/user/uploadExcel',
        elem: '#uploadexcel',
        method: 'post',
        accept:'file',
        exts:'xls|xlsx',
        done: function(res){
            var responseObj=eval('('+'res'+')');
            $('#filetip').text(responseObj.name);
            $('#filepath').text(responseObj.path);
        }
    });
});

// 分页
layui.use('laypage', function() {
    var laypage = layui.laypage;
    laypage.render({
        elem: 'pagehelper',
        limit: $('#pagecount').val(),
        curr:$('#pageindex').val(),
        count:3,
        skip: true,
        jump:function (obj,first) {
            //判断是否首次进入页面
            if(first!=true){
                var pageIndex = obj.curr; //得到了当前页，用于向服务端请求对应数据
                window.location.href='/admin/user/userlist?pageindex='+pageIndex;
            }
        }
    });
});

